class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        if (nums.size() < 3) {
            return 0;
        }
        sort(nums.begin(), nums.end());
        int count = 0;
        int i, j, k;
        for (i = 0; i < nums.size() - 2; i++) {
            k = i + 2;
            for (j = i + 1; j < nums.size() - 1; j++) {
                if (j == k) {
                    k = j + 1;
                }
                while (k < nums.size() && nums[i] + nums[j] > nums[k]) {
                    k++;
                }
                count += k - j - 1;
            }
        }
        return count;
    }
};